<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <view class="jtop_c">成员角色</view>
      <view class="jtop_r" @tap="navigateBack">完成</view>
    </view>

    <view class="tophr"></view>

    <view class="ptt">
      <view class="ptft" @tap="add">
        <image src="../../static/img/index/ic_add_1.png"></image>
        <text>添加角色</text>
      </view>
    </view>

    <view class="pta">
      <view class="" v-for="(item, index) in items" :key="item.id">
        <view class="ptat">
          {{ item.name }}
        </view>
        <view class="ptaf">
          <view class="ptac">
            <view v-for="(itemaa, index1) in item.userList" :key="itemaa.id">
              <view class="jhead_portrait">
                <image
                  :src="
                    itemaa.avatar
                      ? itemaa.avatar
                      : '../../static/img/Default_avatar.png'
                  "
                  class="txl"
                ></image>
                <view class="txct">
                  {{ itemaa.realName }}
                </view>
              </view>
            </view>

            <view class="jhead_portrait" @click="addcy(item.id)">
              <image
                src="../../static/img/index/ic_add_gray.png"
                class="txl"
              ></image>
            </view>
          </view>

          <image
            src="../../static/img/jt.png"
            class="pttjt"
            @click="jsj(item.id, item.name)"
          ></image>
        </view>
      </view>

      <!-- <view class="ptat">
				总经理
			</view>
			<view class="ptaf">

					<view class="ptac">
						<view class="jhead_portrait">
								<image src="../../static/img/jhead_portrait.png" class="txl"></image>
								<view class="txct">
									唐飞
								</view>
						</view>
						<view class="jhead_portrait">
								<image src="../../static/img/jhead_portrait.png" class="txl"></image>
								<view class="txct">
									唐飞
								</view>
						</view>
						<view class="jhead_portrait">
								<image src="../../static/img/jhead_portrait.png" class="txl"></image>
								<view class="txct">
									唐飞
								</view>
						</view>
						<view class="jhead_portrait">
								<image src="../../static/img/index/ic_add_gray.png" class="txl"></image>
						</view>
					</view>

					<image src="../../static/img/jt.png"  class="pttjt"></image>
			
			</view>
			
			 -->
    </view>

    <!-- 弹出层 -->
    <view :class="[show_xl ? 'tc' : 'tc hide']">
      <view class="jtop_bj" @tap="add"></view>
      <view class="tc_box">
        <view class="tc_boxt">
          {{ tt }}
        </view>
        <view class="tc_boxtc">
          请输入角色名称
        </view>
        <view class="tc_boxi">
          <input
            type="text"
            placeholder="角色名称"
            placeholder-style="color:#CCCCCC"
            :value="js"
            @input="onKeyInput_o"
          />
        </view>
        <view class="tc_f">
          <view class="tc_fl" @tap="add">取消</view>
          <view class="tc_fr" @tap="jsave()" :class="ky ? 'no' : ''">确定</view>
        </view>
      </view>
    </view>

    <!-- 弹出层 -->
    <view :class="[ashowpop ? 'tc' : 'tc hide']">
      <view class="jtop_bj" @click="jsj"></view>

      <view class="tc_fbox">
        <view class="tc_fbox_bt2" @click="scjs">
          删除角色
        </view>
        <view class="tc_fbox_bt" @click="jeditjs">
          修改角色
        </view>

        <view class="tc_fbox_bt" @click="jsj">
          取消
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState } from 'vuex';

export default {
  computed: mapState(['forcedLogin', 'hasLogin', 'userName']),
  data() {
    return {
      repeat_click: true,
      show_xl: false,
      js: '',
      ky: true,
      id: '',
      items: [],
      ashowpop: false,
      jqeditid: '',
      jqeditname: '',
      tt: '添加角色',
    };
  },
  onLoad(e) {
    this.id = e.id;
  },
  onShow() {
    this.lb();
  },
  methods: {
    jeditjs() {
      this.js = this.jqeditname;
      this.ashowpop = !this.ashowpop;
      this.show_xl = !this.show_xl;
      this.tt = '编辑角色';
    },

    scjs() {
      // 获取项目角色
      request({
        url: `/ucenter/project-role/${this.jqeditid}`,
        method: 'DELETE',

        success: res => {
          if (res.data.success == true) {
            var that = this;
            wx.showToast({
              title: '删除成功',
              icon: 'none',
              duration: 1000,
              success: function() {
                setTimeout(function() {
                  that.lb();
                  that.ashowpop = !that.ashowpop;
                }, 1500);
              },
            });
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: res.data.msg,
              icon: 'none',
            });
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },
    jsj(e, a) {
      this.jqeditid = e;
      this.jqeditname = a;
      this.ashowpop = !this.ashowpop;
    },
    addcy(e) {
      uni.navigateTo({
        url: '../main/doc_user_selectt?id=' + this.id + '&jsid=' + e,
      });
    },
    onKeyInput_o: function(event) {
      this.js = event.detail.value;
      if (this.js != '') {
        this.ky = false;
      } else {
        this.ky = true;
      }
    },

    add() {
      this.tt = '新增角色';
      this.show_xl = !this.show_xl;
    },

    jsave() {
      if (!this.repeat_click) {
        return;
      }
      this.repeat_click = false;
      if (!this.js) {
        this.repeat_click = true;
        uni.showToast({
          title: '请输入角色名称',
          icon: 'none',
        });
        return;
      }

      if (this.tt == '新增角色') {
        request({
          url: `/ucenter/project-role`,
          data: {
            projectId: this.id,
            name: this.js,
          },

          method: 'POST',
          success: openIdRes => {
            console.log(openIdRes);
            if (openIdRes.data.success == true) {
              var that = this;
              wx.showToast({
                title: '添加成功',
                icon: 'none',
                duration: 1000,
                success: function() {
                  setTimeout(function() {
                    that.lb();
                    that.show_xl = !that.show_xl;
                  }, 1500);
                },
              });

              // uni.navigateBack()
            } else {
              this.repeat_click = true;
              uni.showToast({
                title: openIdRes.data.msg,
                icon: 'none',
              });
              this.isLoading = false;
            }
          },
          fail: () => {
            this.repeat_click = true;
            uni.showToast({
              title: '服务器开小差，请稍后~',
              icon: 'none',
            });
            return false;
          },
        });
      } else {
        request({
          url: `/ucenter/project-role/${this.jqeditid}`,
          data: {
            // id:this.jqeditid,
            name: this.js,
          },

          method: 'PUT',
          success: openIdRes => {
            console.log(openIdRes);
            if (openIdRes.data.success == true) {
              var that = this;
              wx.showToast({
                title: '修改成功',
                icon: 'none',
                duration: 1000,
                success: function() {
                  setTimeout(function() {
                    that.lb();
                    that.show_xl = !that.show_xl;
                  }, 1500);
                },
              });

              // uni.navigateBack()
            } else {
              this.repeat_click = true;
              uni.showToast({
                title: openIdRes.data.msg,
                icon: 'none',
              });
              this.isLoading = false;
            }
          },
          fail: () => {
            this.repeat_click = true;
            uni.showToast({
              title: '服务器开小差，请稍后~',
              icon: 'none',
            });
            return false;
          },
        });
      }
    },

    lb() {
      // 获取项目角色
      request({
        url: `/ucenter/project-role/${this.id}`,
        method: 'GET',

        success: res => {
          console.log('项目项目角色', res);
          console.log(res.data.data.length);
          if (res.data.success == true) {
            this.items = res.data.data;
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: res.data.msg,
              icon: 'none',
            });
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },
    navigateBack() {
      uni.navigateBack();
    },
  },
};
</script>

<style>
.tophr {
  width: 100%;
  height: 88rpx;
}
.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9;
}
.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}
.tc_box {
  width: 540rpx;
  height: 400rpx;
  background: #ffffff;
  border-radius: 14rpx;
  margin: 460rpx 105rpx 0;
  position: relative;
  z-index: 10001;
  overflow: hidden;
}
.tc_boxt {
  width: 100%;
  height: 44rpx;
  font-size: 36rpx;
  line-height: 44rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 40rpx 0 20rpx;
  color: #000000;
}
.tc_boxtc {
  width: 100%;
  height: 36rpx;
  font-size: 28rpx;
  line-height: 36rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 0rpx 0 30rpx;
  color: #666666;
}
.tc_boxi {
  width: 460rpx;
  height: 88rpx;
  background: #ffffff;
  border-radius: 8rpx;
  border: 1px solid #dddddd;
  display: block;
  float: left;
  margin: 0rpx 40rpx 42rpx 40rpx;
}
.tc_boxi input {
  width: 90%;
  height: 48rpx;
  margin: 20rpx 5%;
  font-size: 32rpx;
  color: #000000;
}
.tc_f {
  width: 100%;
  height: 100rpx;
  display: block;
  float: left;
  border-top: 1px solid #dcdee3;
}
.tc_fl {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
  border-right: 1px solid #dcdee3;
}
.tc_fr {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
}
.tc_fr.no {
  opacity: 0.5;
}

.ptt {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.pttl {
  width: 84rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 20rpx 18rpx 30rpx;
}
.pttr {
  width: 400rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 0rpx 0rpx 0rpx;
}
.pttrt {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 34rpx;
  color: #000000;
}
.pttrf {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 24rpx;
  color: #999999;
}
.pttjt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 48rpx 30rpx 0 30rpx;
}
.ptf {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.ptft {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
}
.ptft image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 21rpx 20rpx 0 0rpx;
}
.ptft text {
  width: auto;
  height: 48rpx;
  display: block;
  float: left;
  margin: 21rpx 20rpx 0 0rpx;
  font-size: 34rpx;
  line-height: 48rpx;
  color: #000000;
}

.pta {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.ptat {
  width: 100%;
  height: 32rpx;
  line-height: 32rpx;
  display: block;
  float: left;
  font-size: 24rpx;
  color: #000000;
  margin: 15rpx 30rpx;
}
.ptaf {
  width: 720rpx;
  height: auto;
  display: block;
  float: right;
  border-bottom: 1px solid #dddddd;
}
.ptac {
  width: 650rpx;
  height: auto;
  display: block;
  float: left;
}
.jhead_portrait {
  width: 84rpx;
  height: auto;
  display: block;
  float: left;
  margin: 0rpx 20rpx 20rpx 0;
}
.txl {
  width: 84rpx;
  height: 84rpx;
  display: block;
  float: left;
  border-radius: 6rpx;
}
.pttjt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 50rpx 30rpx 0 20rpx;
}
.txc {
  width: 400rpx;
  height: auto;
  display: block;
  float: left;
  margin: 0;
}
.txct {
  width: 84rpx;
  height: 32rpx;
  text-align: center;
  overflow: hidden;
  line-height: 32rpx;
  display: block;
  float: left;
  font-size: 24rpx;
  color: #999999;
  margin: 10rpx 0 0 0;
}

.addi input {
  width: 580rpx;
  height: 50rpx;
  display: block;
  float: left;
  margin: 24rpx 30rpx 24rpx 30rpx;
  text-align: left;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #000000;
}
.addi image {
  width: 36rpx;
  height: 36rpx;
  display: block;
  float: right;
  margin: 30rpx 20rpx 0 0rpx;
}

.addb {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  margin: 20rpx 0 0;
}
.addbi {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.addb .addbi:last-child {
  border: none;
}
.addbi .addbil {
  width: auto;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
}
.addbi .addbir {
  width: auto;
  height: 90rpx;
  display: block;
  float: right;
  line-height: 90rpx;
  font-size: 32rpx;
  color: #999999;
}
.addbi image {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 33rpx 30rpx 0 30rpx;
}
.addi textarea {
  width: 690rpx;
  height: 150rpx;
  display: block;
  float: left;
  margin: 0rpx 30rpx 20rpx;
  text-align: left;
  line-height: 40rpx;
  font-size: 34rpx;
  color: #999999;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #256aff;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}

.jtop_bs {
  width: 100%;
  height: auto;
  background: #ffffff;
  display: block;
  float: left;
}
.jtop_bs view {
  width: 720rpx;
  height: 90rpx;
  background: #ffffff;
  display: block;
  float: left;
  margin: 0 0 0 30rpx;
  border-bottom: 1px solid #dddddd;
}
.jtop_bs view text {
  width: 80%;
  height: 90rpx;
  line-height: 90rpx;
  background: #ffffff;
  display: block;
  float: left;
  font-size: 34rpx;
  color: #000000;
}
.jtop_bs view image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 21rpx 30rpx 0 0;
}

.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
  background: #ffffff;
}
.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #f2f2f2;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}
.tc_fbox_bt2 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: red;
  border-top: 0rpx solid #f2f2f2;
  background: #ffffff;
}
</style>
